const { orderList, goodList } = require("../../../utils/api");

// pages/goodslist/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    "tabList": [
      {
        "id": "0",
        "name": "全部订单",
        "status": "9"
      },
      {
        "id": "1",
        "name": "待发货",
        "status": "0"
      },
      {
        "id": "2",
        "name": "待收货",
        "status": "1"
      },
      {
        "id": "3",
        "name": "已完成",
        "status": "2"
      }
    ],
    activeid: 0,
    goodsList: [],
    showSure: false,
    orderStatus: '',
    goodsId: '',
    page: 1,
    limit: 10,
    count: null,
    onBottom: false
  },

  /**
 * 生命周期函数--监听页面加载
 */
  onLoad(options) {

    this.setData({
      activeid: options.id,
      orderStatus: options.status
    })
    this.getList()

  },

  getList() {
    let that = this
    that.setData({
      //  goodsList: [],
      count: null
    })
    let params = {
      status: that.data.orderStatus,
      page: that.data.page,
      limit: that.data.limit,
    }
    orderList(params).then((res) => {

      if (res.errcode == 0) {
        let resData = res.data.orderlist.orders

        if (that.data.onBottom == false) {

          that.setData({
            count: res.data.orderlist.count,
            goodsList: [...resData],
          })
        } else {
          let newArr = [...that.data.goodsList, ...resData]
          that.setData({
            count: res.data.orderlist.count,
            goodsList: newArr,
          })
        }

      }
    })
  },


  tabclick(e) {
    const index = e.currentTarget.dataset.item.id;
    this.setData({
      activeid: index,
      orderStatus: e.currentTarget.dataset.item.status
    })
    this.setData({
      page: 1,
      onBottom: false,
      goodsList: []
    })
    this.getList()
  },

  toOrderDetail(e) {
    wx.navigateTo({
      url: `/packageA/pages/orderdetail/index?orderid=${e.currentTarget.dataset.item.id}`,
    })
  },

  //查看物流信息
  toLogistic(e) {
    wx.navigateTo({
      url: `/pages/logisticsinfo/index?orderid=${e.currentTarget.dataset.item.id}`,
    })
  },

  //再次兑换
  changeAgain(e) {
    wx.reLaunch({
      url: `/packageA/pages/goodsdetail/index?goodsid=${e.currentTarget.dataset.id}`,
    })

  },


  //确认收货
  confirmGoods(e) {
    this.setData({
      goodsId: e.currentTarget.dataset.id,
      showSure: true
    })

  },

  //补件
  toBujian(e) {


    wx.navigateToMiniProgram({
      appId: 'wxcd2b42bc52a3935f',
      path: e.currentTarget.dataset.item.goods.inputurl,
      success(res) {

      }
    })
  },




  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {


  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },



  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.page * this.data.limit >= this.data.count) {
      this.setData({
        onBottom: false
      })
      wx.showToast({
        title: '没有更多了',
        icon: 'none',

      });

    } else {
      this.setData({
        onBottom: true
      })
      this.data.page++
      this.getList()
    }


  },

})